﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit.Electric.Common;

namespace Model
{
	public partial class DlgCableTrayAlignment : System.Windows.Forms.Form
	{
		public DlgCableTrayAlignment()
		{
			this.InitializeComponent();
		}

		private void DlgCableTrayAlignment_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.xmlHelper.LoadXml(this.path, "Root");
			this.LoadDlgData();
		}

		private void DlgCableTrayAlignment_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
			this.SendESC();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SendESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgCableTrayAlignment.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgCableTrayAlignment.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		public void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayAlignment"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayAlignment", "Params"));
			this.xmlHelper.SetAttribute("BottomAlign", this.chkVerticalBottomAlignment.Checked.ToString());
			this.xmlHelper.SetAttribute("HorizontalAlign", this.chkHorizontalAlignment.Checked.ToString());
			string value = "0";
			if (this.rdoAlign.Checked)
			{
				value = "1";
			}
			else if (this.rdoRightAlign.Checked)
			{
				value = "2";
			}
			this.xmlHelper.SetAttribute("AligMethod", value);
			this.xmlHelper.SaveXml("");
		}

		public void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayAlignment", "Params"));
			string attribute = this.xmlHelper.GetAttribute("BottomAlign");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.chkVerticalBottomAlignment.Checked = Convert.ToBoolean(attribute);
			}
			attribute = this.xmlHelper.GetAttribute("HorizontalAlign");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.chkHorizontalAlignment.Checked = Convert.ToBoolean(attribute);
			}
			attribute = this.xmlHelper.GetAttribute("AligMethod");
			if (!string.IsNullOrEmpty(attribute))
			{
				if (attribute == "0")
				{
					this.rdoLeftAlignment.Checked = true;
					return;
				}
				if (attribute == "1")
				{
					this.rdoAlign.Checked = true;
					return;
				}
				this.rdoRightAlign.Checked = true;
			}
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DlgCableTrayAlignment_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgCableTrayAlignment.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void chkHorizontalAlignment_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkHorizontalAlignment.Checked)
			{
				this.rdoLeftAlignment.Enabled = true;
				this.rdoAlign.Enabled = true;
				this.rdoRightAlign.Enabled = true;
				return;
			}
			this.rdoLeftAlignment.Enabled = false;
			this.rdoAlign.Enabled = false;
			this.rdoRightAlign.Enabled = false;
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CableTrayAlignment.xml");

		private const string Root = "Root";

		private const string Node = "HYCableTrayAlignment";

		private const string ChildNode = "Params";

		private const string BottomAlign = "BottomAlign";

		private const string HorizontalAlign = "HorizontalAlign";

		private const string AligMethod = "AligMethod";
	}
}
